from django.shortcuts import render,HttpResponse,redirect,reverse
from django.urls import reverse
from app01 import models
from django import forms
# Create your views here.
def user(request):
    user_all = models.User.objects.all()
    return render(request,'user.html',{'user_all':user_all})

def lab(request):
    lab_all = models.Lab.objects.all()
    return render(request,'lab.html',{'lab_all':lab_all})

def asset(request):
    asset_all = models.Asset.objects.all()
    return render(request,'asset.html',{'asset_all':asset_all})

class Asset_add(forms.Form):
    name = forms.CharField(label='物资名称',error_messages={'required':'不能为空'})
    describe = forms.CharField(label='物资描述',error_messages={'required':'不能为空'})
    num = forms.CharField(label='物资编号',error_messages={'required':'不能为空'})
    username = forms.MultipleChoiceField(label='购买人', choices=models.User.objects.values_list('id', 'user'))
class User_add(forms.Form):
    user = forms.CharField(label='用户名',error_messages={'required':'不能为空'})
    name = forms.CharField(label='姓名',error_messages={'required':'不能为空'})
    pwd = forms.CharField(label='密码',error_messages={'required':'不能为空'},widget=forms.PasswordInput)
    phon = forms.CharField(label='手机号',error_messages={'required':'不能为空'})
class Lab_add(forms.Form):
    name = forms.CharField(label='实验室名称',error_messages={'required':'不能为空'})
    floor = forms.IntegerField(label='楼层',error_messages={'required':'不能为空'})
    room = forms.IntegerField(label='房间号',error_messages={'required':'不能为空'})
    username = forms.MultipleChoiceField(label='负责人', choices=models.User.objects.values_list('id', 'user'))


def asset_add(request):
    if request.method=='POST':
        form_obj = Asset_add(request.POST)
        if form_obj.is_valid():
            name = request.POST.get('name')
            describe = request.POST.get('describe')
            num = request.POST.get('num')
            id = request.POST.getlist('username')

            if models.Asset.objects.filter(name=name):
                return render(request,'asset_add.html',{'form_obj':form_obj,'error':'物资以存在'})
            else:
                te = models.Asset.objects.create(name= name,describe=describe,num=num)
                te.save()
                te.people.set(id)
                return redirect(reverse('asset'))
    else:
        form_obj=Asset_add()
    return render(request,'asset_add.html',{'form_obj':form_obj})
def user_add(request):
    if request.method=='POST':
        form_obj = User_add(request.POST)
        if form_obj.is_valid():
            user = request.POST.get('user')
            name = request.POST.get('name')
            pwd = request.POST.get('pwd')
            phon = request.POST.get('phon')
            if models.User.objects.filter(user=user):
                return render(request,'user_add.html',{'error':'用户以存在'})
            else:
                models.User.objects.create(name= name,user=user,pwd=pwd,phon=phon)
                return redirect(reverse('user'))
    else:
        form_obj=User_add()

    return render(request,'user_add.html',{'form_obj':form_obj})
def lab_add(request):
    if request.method=='POST':
        form_obj = Lab_add(request.POST)
        if form_obj.is_valid():
            name = request.POST.get('name')
            floor = request.POST.get('floor')
            room = request.POST.get('room')
            username = request.POST.getlist('username')
            if models.Lab.objects.filter(name=name):
                return render(request,'lab_add.html',{'form_obj':form_obj,'error':'实验室以存在'})
            else:
                te = models.Lab.objects.create(name=name,floor=floor,room=room)
                te.save()
                te.username.set(username)
                return redirect(reverse('lab'))
    else:
        form_obj = Lab_add()
        return render(request, 'lab_add.html', {'form_obj': form_obj})
def a_remove(request):
    id = request.POST.get('k1')
    cl = request.POST.get('k2')
    print(id,cl)
    for i in models.list:
        if i.__name__==cl:
            i.objects.filter(pk=id).delete()
            return HttpResponse('ok')


def asset_edit(request):
    id = request.GET.get('id')
    asset_dan= models.Asset.objects.filter(id=id)[0]
    people_all = models.User.objects.all()
    if request.method=='POST':
        name = request.POST.get('name')
        describe = request.POST.get('describe')
        num = request.POST.get('num')
        people = request.POST.getlist('people')
        asset_dan.name=name
        asset_dan.describe=describe
        asset_dan.num=num
        asset_dan.save()
        asset_dan.people.set(people)
        return redirect(reverse('asset'))
    return render(request,'asset_edit.html',{'asset_dan':asset_dan,'people_all':people_all})

def lab_edit(request):
    id = request.GET.get('id')
    lab_dan= models.Lab.objects.filter(id=id)[0]
    people_all = models.User.objects.all()
    if request.method=='POST':
        name = request.POST.get('name')
        floor = request.POST.get('floor')
        room = request.POST.get('room')
        people = request.POST.getlist('people')
        lab_dan.name=name
        lab_dan.floor=floor
        lab_dan.room=room
        lab_dan.save()
        lab_dan.username.set(people)
        return redirect(reverse('lab'))
    return render(request,'lab_edit.html',{'lab_dan':lab_dan,'people_all':people_all})
def user_edit(request):
    id = request.GET.get('id')
    user_dan= models.User.objects.filter(id=id)[0]
    if request.method=='POST':
        user = request.POST.get('user')
        name = request.POST.get('name')
        pwd = request.POST.get('pwd')
        phon = request.POST.get('phon')
        user_dan.user=user
        user_dan.name=name
        user_dan.pwd=pwd
        user_dan.phon=phon
        user_dan.save()
        return redirect(reverse('user'))

    return render(request,'user_edit.html',{'user_dan':user_dan})